package hibernate;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;

public class LoginController {
	@FXML
	private TextField ID;
	@FXML
	private TextField password;
	@FXML
	private Button sbtn;
	@FXML
	private Button tbtn;
	@FXML
	private Button mbtn;
	
	
	DbsOperation op = null;
	@FXML
	private void initialize() {
		op = new DbsOperation();
	}
	@FXML
	private void s_login_click() throws SQLException {
		Integer s_ID = Integer.parseInt(ID.getText());
		String pwd = password.getText();
		Student student = op.getStudent(s_ID);
		if(student == null || !student.getPwd().equals(pwd)) ;
		else LoadStudentScene(student);
		

		
	}
	@FXML
	private void t_login_click() throws SQLException {
		Integer t_ID = Integer.parseInt(ID.getText());
		String pwd = password.getText();
		Teacher teacher = op.getTeacher(t_ID);
		if(teacher == null || !teacher.getPwd().equals(pwd)) ;
		else LoadTeacherScene(teacher);
		
	}
	
	@FXML	
	private void m_login_click() {
		String s_ID = ID.getText();
		String pwd = password.getText();
		if(s_ID.contentEquals("root") && pwd.contentEquals("admin")) {
			LoadManagerScene();
		}
	}
	public void LoadStudentScene(Student stu) {
		try {
			FXMLLoader loader = new FXMLLoader();
			StudentController.setStudent(stu);
            loader.setLocation( LoginMainScene.class.getResource("StudentScene.fxml"));
            LoginMainScene.rootLayout = (Pane) loader.load();
			
			Scene scene = new Scene(LoginMainScene.rootLayout);
			LoginMainScene.primaryStage.setScene(scene);
			
//			LoginMainScene.primaryStage.show();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	public void LoadTeacherScene(Teacher teacher) {
		try {
			FXMLLoader loader = new FXMLLoader();
			System.out.println("load " + teacher);
			TeacherController.setTeacher(teacher);
            loader.setLocation( LoginMainScene.class.getResource("TeacherScene.fxml"));
            LoginMainScene.rootLayout = (Pane) loader.load();
			
			Scene scene = new Scene(LoginMainScene.rootLayout);
			LoginMainScene.primaryStage.setScene(scene);

		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public void LoadManagerScene() {
		try {
			FXMLLoader loader = new FXMLLoader();
            loader.setLocation( LoginMainScene.class.getResource("ManagerScene.fxml"));
            LoginMainScene.rootLayout = (Pane) loader.load();
			
			Scene scene = new Scene(LoginMainScene.rootLayout);
			LoginMainScene.primaryStage.setScene(scene);

		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}
